package com.myfum.wingplayer.components.decorators
{
	import com.myfum.wingplayer.components.containers.AbstractContainer;
	
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	public class AudioDecorator extends AbstractDecorator
	{
		private var sound:Sound;
		private var channel:SoundChannel;
		
		public function AudioDecorator(container:AbstractContainer)
		{
			super(container);
		}
		
		override public function getReady():void{
			sound = new Sound(new URLRequest(container.data.path));
			sound.addEventListener(Event.COMPLETE, onLoadComplete);
		}
		
		private function onLoadComplete(e:Event):void{
			startPlay();
			dispatchEvent(e);
		}
		
		private function startPlay():void{
			var trans:SoundTransform = new SoundTransform();
			var vol:Number = mute ? 0:1;
			channel = sound.play();
			channel.soundTransform = trans;
		}
		
		override public function dispose():void{
			if(channel){
				channel.stop();
			}
			if(sound){
				sound.removeEventListener(Event.COMPLETE, onLoadComplete);
				sound.close();
			}
			super.dispose();
		}
	}
}